package com.zyw.auth.common;

import com.zyw.auth.domain.Administrator;
import com.zyw.auth.shiro.realm.LxbAdminRealm;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.mgt.RealmSecurityManager;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.PrincipalCollection;

public final class ShiroAuthHelper {
    private ShiroAuthHelper() {
    }

    /**
     * <p>清空shiro(当前登录)用户证认/授权缓存</p>
     * <p>目前只缓存授权，认证因只登录一次且未实现会话持久话（分布式会话）故没有缓存认证</p>
     * <p>可用于用户登录前，或注销后 清空授权缓存信息</p>
     */
    public static void clearCurUserCachedAuthInfo() {
        PrincipalCollection principals = SecurityUtils.getSubject().getPrincipals();
        RealmSecurityManager securityManager = (RealmSecurityManager) SecurityUtils.getSecurityManager();
        securityManager.getRealms().forEach(realm -> {
            if (realm instanceof LxbAdminRealm) {
                ((LxbAdminRealm) realm).clearCachedAuthorizationInfo(principals);
            }
        });
    }

    /**
     * <p>清空shiro(所有)用户证认/授权缓存</p>
     * <p>目前只缓存授权，认证因只登录一次且未实现会话持久话（分布式会话）故没有缓存认证</p>
     * <p>用于修改角色权限时调用，清除所有用户权限</p>
     */
    public static void clearAllCachedAuthInfo() {
        RealmSecurityManager securityManager = (RealmSecurityManager) SecurityUtils.getSecurityManager();
        securityManager.getRealms().forEach(realm -> {
            if (realm instanceof LxbAdminRealm) {
                ((LxbAdminRealm) realm).clearAllCachedAuthorizationInfo();
            }
        });
    }

    /**
     * 获取当前登录用户
     */
    public static Administrator getCurrentAdmin() {
        return (Administrator) SecurityUtils.getSubject().getPrincipal();
    }

    /**
     * 获取当前登录用户ID
     */
    public static Long getCurrentAdminId() {
        Administrator admin = getCurrentAdmin();
        if (admin != null) {
            return admin.getAdminId();
        }
        return null;
    }

    public static Session getCurrentSession() {
        return SecurityUtils.getSubject().getSession(true);
    }

}
